import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';


import {
  apibefore
} from '../../router'
import { UserSettingService } from './userSetting.service';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { UserSetting } from './userSetting.entity';

@Controller(apibefore('/user_setting'))
@UseInterceptors(ValidTokenInterceptor)
@UseInterceptors(ValidRSAInterceptor)
export class UserSettingController {
  constructor(private readonly settingService: UserSettingService) {}

  @Get('/get_isvideo')
  async get_isvideo(@Query() params, @Request() req){
    if(req.headers.wx_user == 'oGCIM6xIWkOgZ_QxejB3TrMl1-MQ' || req.headers.wx_user == 'oGCIM6wIzmDJoqPBYsci5JgpWsHY') return true
    let result = await this.settingService.get_isvideo(req.headers.wx_user, params.overseas)
    if(result){
      let content = '请先清空作品再进行操作。认证后仅支持国内作者发布本人作品。'
      if(params.overseas == 1){
        content = '请先清空作品再进行操作。'
      }
      return new HttpException(content,HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }

  @Get('/get')
  async get(@Query() params, @Request() req){
    return await this.settingService.findOne_by_key(req.headers.wx_user, params.key)
  }

  @Get('/getIsSwitchOverseas')
  async getIsSwitchOverseas(@Query() params, @Request() req){
    // if(req.headers.wx_user == 'oGCIM6xIWkOgZ_QxejB3TrMl1-MQ' || req.headers.wx_user == 'oGCIM6wIzmDJoqPBYsci5JgpWsHY') return true
    return await this.settingService.getIsSwitchOverseas(req.headers.wx_user)
  }

  @Post('/setoverseas')
  async setoverseas(@Body() data, @Request() req){
    let wx_user = req.headers.wx_user
    let key = 'overseas'
    let setting = await this.settingService.findOne_by_key(wx_user, key)
    if(!setting){
      setting = new UserSetting()
      setting.wx_user = wx_user
      setting.key = key
    }
    setting.value = data.value
    if(!setting.id){
      let err = await this.settingService.create(setting)
      if(err){
        return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
      }else{
        return true
      }
    }else{
      let err = ''
      if(setting.value == '1'){
        err = await this.settingService.update(setting.wx_user, setting.key, setting.value)
      }else{
        err = await this.settingService.changeOverseas(setting.wx_user, setting.value)
      }
      if(err){
        return new HttpException('修改失败', HttpStatus.INTERNAL_SERVER_ERROR)
      }else{
        return true
      }
    }
  }

  @Post('/set')
  async set(@Body() data, @Request() req){
    let wx_user = req.headers.wx_user
    let setting = await this.settingService.findOne_by_key(wx_user, data.key)
    if(!setting){
      setting = new UserSetting()
      setting.wx_user = wx_user
      setting.key = data.key
    }
    setting.value = data.value
    if(!setting.id){
      return await this.settingService.create(setting)
    }else{
      let result = await this.settingService.update(setting.wx_user, setting.key, setting.value)
      if(result.affected > 0){
        return setting
      }else{
        return new HttpException('修改失败', HttpStatus.INTERNAL_SERVER_ERROR)
      }
    }
  }
}
